<?php

	class Product extends AppModel {
		var $name = 'Product';
		var $useTable = 'products';
		var $primaryKey = 'id';
		var $displayField = 'code';
		
		var $controller;
		
		var $hasOne = array(
			'ProductDescription'
		);
		
		var $hasMany = array(
			'ProductImage',
			//'ProductDescription', // Multi idioma
			'ProductTaxonomy'
		);
		
		var $belongsTo = array(
			'Shop',
			'Currency'
		);
		
		var $hasAndBelongsToMany = array(
			'Taxonomy' => array(
				'className' => 'Taxonomy',
				'joinTable' => 'products_taxonomies',
				'foreignKey' => 'product_id',
				'associationForeignKey' => 'taxonomy_id'
			)
		);
		
		var $virtualFields = array();
		
		function __construct($id = false, $table = null, $ds = null) {
			parent::__construct($id, $table, $ds);
			$this->virtualFields['preview_pico']		= 'CONCAT(\''. Configure::read('Site.Url.image') . '\', REPLACE(Product.preview,\'.\',\'_pico.\'))';
			$this->virtualFields['preview_icon']		= 'CONCAT(\''. Configure::read('Site.Url.image') . '\', REPLACE(Product.preview,\'.\',\'_icon.\'))';
			$this->virtualFields['preview_tiny']		= 'CONCAT(\''. Configure::read('Site.Url.image') . '\', REPLACE(Product.preview,\'.\',\'_tiny.\'))';
			$this->virtualFields['preview_small']		= 'CONCAT(\''. Configure::read('Site.Url.image') . '\', REPLACE(Product.preview,\'.\',\'_small.\'))';
			$this->virtualFields['preview_compact']		= 'CONCAT(\''. Configure::read('Site.Url.image') . '\', REPLACE(Product.preview,\'.\',\'_compact.\'))';
			$this->virtualFields['preview_medium']		= 'CONCAT(\''. Configure::read('Site.Url.image') . '\', REPLACE(Product.preview,\'.\',\'_medium.\'))';
			$this->virtualFields['preview_large']		= 'CONCAT(\''. Configure::read('Site.Url.image') . '\', REPLACE(Product.preview,\'.\',\'_large.\'))';
			$this->virtualFields['preview_grande']		= 'CONCAT(\''. Configure::read('Site.Url.image') . '\', REPLACE(Product.preview,\'.\',\'_grande.\'))';
			$this->virtualFields['preview_original']	= 'CONCAT(\''. Configure::read('Site.Url.image') . '\', Product.preview)';
		}
		
		function details($id = 0, $slug = null) {
			$details = $this->find(
				'first',
				array(
					'conditions' => array(
						array(
							'or' => array(
								'ProductDescription.slug' => $slug,
								'ProductDescription.id' => $id
							)
						),
						'Product.shop_id' => Configure::read('Shop.id')
					),
					'fields' => array(
						'Product.quantity',
						'Product.on_sale',
						'Product.price',
						'Product.show_price',
						'Product.model',
						'Product.code',
						'Product.width',
						'Product.height',
						'Product.depth',
						'Product.weight',
						'Product.preview',
						'Product.preview_pico',
						'Product.preview_icon',
						'Product.preview_tiny',
						'Product.preview_small',
						'Product.preview_compact',
						'Product.preview_medium',
						'Product.preview_large',
						'Product.preview_grande',
						'Product.preview_original',
						'Product.condition',
						'Product.available'
					),
					'contain' => array(
						'ProductDescription' => array(
							'fields' => array(
								'ProductDescription.slug',
								'ProductDescription.name',
								'ProductDescription.description',
								'ProductDescription.reference_url'
							)
						),
						'ProductImage' => array(
							'fields' => array(
								'ProductImage.pico',
								'ProductImage.icon',
								'ProductImage.tiny',
								'ProductImage.small',
								'ProductImage.medium',
								'ProductImage.large',
								'ProductImage.grande',
								'ProductImage.original',
							)
						),
						'Currency' => array(
							'fields' => array(
								'Currency.code',
								'Currency.symbol_left',
								'Currency.symbol_right',
								'Currency.decimal_places',
								'Currency.decimals',
								'Currency.thousands'
							)
						),
						'Taxonomy' => array(
							'fields' => array(
								'Taxonomy.name'
							)
						),
						'ProductImage'
					)
				)
			);
			
			if (empty($details)) {
				return false;
			}
			
			$images = array();
			$images['ProductImage'] = $this->ProductImage->thumbnails($details['Product']['id']);
			
			return array_merge($details, $images);
		}
		
		function collections(&$controller, $params = array()) {
			$controller->helpers[] = 'Number';
			$controller->paginate = array(
				'conditions' => array(
					'Product.shop_id' => Configure::read('Shop.id')
				),
				'fields' => array(
					'Product.id',
					'Product.quantity',
					'Product.on_sale',
					'Product.price',
					'Product.show_price',
					'Product.model',
					'Product.code',
					'Product.width',
					'Product.height',
					'Product.depth',
					'Product.weight',
					'Product.preview',
					'Product.preview_pico',
					'Product.preview_icon',
					'Product.preview_tiny',
					'Product.preview_small',
					'Product.preview_compact',
					'Product.preview_medium',
					'Product.preview_large',
					'Product.preview_grande',
					'Product.preview_original',
					'Product.condition',
					'Product.available'
				),
				'contain' => array(
					'ProductDescription' => array(
						'fields' => array(
							'ProductDescription.slug',
							'ProductDescription.name',
							'ProductDescription.description',
							'ProductDescription.reference_url'
						)
					),
					'Currency' => array(
						'fields' => array(
							'Currency.code',
							'Currency.symbol_left',
							'Currency.symbol_right',
							'Currency.decimal_places',
							'Currency.decimals',
							'Currency.thousands'
						)
					),
					'Taxonomy' => array(
						'fields' => array(
							'Taxonomy.name'
						)
					)
				),
				'order' => 'Product.created DESC'
			);
			
			$controller->paginate = array_merge($controller->paginate, $params);
			
			return $controller->paginate('Product');
		}
		
		function search($q = null) {
			return $this->find(
				'all',
				array(
					'conditions' => array(
						'LOWER(Product.name) LIKE \'%' . low($q) . '%\''
					)
				)
			);
		}
	}
